﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Threading;
using AEM.Data;
using AEM.Utils;
using Actions = AEM.Data.BLL.Actions;

namespace AEM.Common
{
    public static class PeakMonitoring
    {
        private static DispatcherTimer _peakTimer;
        public static List<PeakItem> peakItems = new List<PeakItem>();
        public static void Init()
        {
            if (null != _peakTimer) _peakTimer.Stop();
            if (null == Config.LiftInftList) return;

            peakItems = new List<PeakItem>();
            DateTime now = DateTime.Now;
            int weekday = (int)now.DayOfWeek;
            List<Data.Model.PeakSetting> plist = Actions.PeakSetting.List(" Weekday=" + weekday);
            if (null != plist)
            {
                var result = from r in plist
                             join Data.Model.LiftInfo l in Config.LiftInftList
                              on r.LiftNum equals l.LiftNum
                             select new
                             {
                                 LiftNum = r.LiftNum,
                                 l.LiftCOM,
                                 TimeFrom = r.TimeFrom,
                                 TimeTo = r.TimeTo
                             };
                foreach (var r in result)
                {
                    string[] tmp = r.TimeFrom.Split(':');
                    int hf = tmp[0].toNumeric();
                    int mf = tmp[1].toNumeric();

                    tmp = r.TimeTo.Split(':');
                    int ht = tmp[0].toNumeric();
                    int mt = tmp[1].toNumeric();

                    PeakItem peak = new PeakItem();
                    peak.LiftNum = r.LiftNum;
                    peak.COM = r.LiftCOM;
                    peak.HourFrom = hf;
                    peak.MinuteFrom = mf;
                    peak.HourTo = ht;
                    peak.MinuteTo = mt;
                    peakItems.Add(peak);
                }
                _peakTimer = new DispatcherTimer();
                _peakTimer.Interval = TimeSpan.FromMinutes(1);
                _peakTimer.Tick += new EventHandler((object sender, EventArgs e) =>
                {
                    DateTime tick = DateTime.Now;
                    foreach (PeakItem p in peakItems)
                    {
                        if ((p.HourTo > tick.Hour || (p.HourTo == tick.Hour && p.MinuteTo > tick.Minute))
                            && (p.HourFrom < tick.Hour || (p.HourFrom == tick.Hour && p.MinuteFrom <= tick.Minute)))
                        {
                            if (!p.IsPeek)
                                if (null != Actions.SerialPorts.Regulate)
                                    Actions.SerialPorts.Regulate.BeginInvoke(true,
                                        p.LiftNum, p.COM, byte.Parse("3", NumberStyles.HexNumber), 0, new AsyncCallback(Actions.SerialPorts.RegulateCallback), null);
                        }
                        else if ((p.HourTo < tick.Hour) || (p.HourTo == tick.Hour && p.MinuteTo <= tick.Minute))
                        {
                            if (p.IsPeek && !p.IsUnpeek)
                                if (null != Actions.SerialPorts.Regulate)
                                    Actions.SerialPorts.Regulate.BeginInvoke(false,
                                        p.LiftNum, p.COM, byte.Parse("3", NumberStyles.HexNumber), 0, new AsyncCallback(Actions.SerialPorts.RegulateCallback), null);
                        }
                    }
                    if (tick.Hour == 0 && tick.Minute < 1)
                        PeakMonitoring.Init();
                });
                _peakTimer.Start();
            }
        }
    }

    /// <summary>
    /// 高峰运行
    /// </summary>
    public class PeakItem
    {
        /// <summary>
        /// 高峰控制是否成功
        /// </summary>
        public bool IsPeek { get; set; }
        /// <summary>
        /// 高峰解除是否成功
        /// </summary>
        public bool IsUnpeek { get; set; }
        public int LiftNum { get; set; }
        public string COM { get; set; }
        public int HourFrom { get; set; }
        public int MinuteFrom { get; set; }
        public int HourTo { get; set; }
        public int MinuteTo { get; set; }

    }
}
